home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-13 | 35.7 KB | 1,219 lines | [TEXT/MPS ] |
- /*------------------------------------------------------------------------------
-
- File: TextEditorMain.cpp
-
- Description: TextEditor's initialization protocol methods.
-
- Written by: Steve Smith
-
- Copyright: © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
-
- ------------------------------------------------------------------------------*/
-
- // Notification that this is the "main" SOM source file
- #define SampleCode_TextEditor_Class_Source
- // define underscore (_) field names
- #define VARIABLE_MACROS
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (e.g. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with "ev" parameter set.
- #include <Except.h>
- #endif
-
- // -- TextEditor Includes
-
- #ifndef SOM_SampleCode_TextEditor_xih
- #include "TextEditor.xih"
- #endif
-
- #ifndef SOM_TextEditorSemIntf_xh
- #include "TextEditorSemIntf.xh"
- #endif
-
- #ifndef SOM_TextEditorSettingsExt_xh
- #include "TextEditorSettingsExt.xh"
- #endif
-
- #ifndef SOM_TextEditorTransferExt_xh
- #include "TextEditorTransferExt.xh"
- #endif
-
- #ifndef _TEXTEDITORDEF_
- #include "TextEditorDef.h"
- #endif
-
- #ifndef _TEXTEDITORUTILS_
- #include "TextEditorUtils.h"
- #endif
-
- #ifndef _TEXTEDITORGLOBALS_
- #include "TextEditorGlobals.h"
- #endif
-
- #ifndef _TEXTEDITORPREFS_
- #include "TextEditorPrefs.h"
- #endif
-
- #ifndef _STDTEXTPROPERTIES_
- #include "StdTextProps.h"
- #endif
-
- // -- OpenDoc Includes --
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_ODPart_xh
- #include <Part.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODDraft_xh
- #include <Draft.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODArbitrator_xh
- #include <Arbitrat.xh>
- #endif
-
- #ifndef SOM_ODFocusSet_xh
- #include <FocusSet.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_Foci_defined
- #include <Foci.xh>
- #endif
-
- #ifndef SOM_ODMenuBar_xh
- #include <MenuBar.xh>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODObjectNameSpace_xh
- #include <ObjectNS.xh>
- #endif
-
- #ifndef SOM_ODTranslation_xh
- #include <Translt.xh>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StandardExtensions_defined
- #include <StdExts.xh>
- #endif
-
- #ifndef SOM_ODSettingsExtension_xh
- #include <Settings.xh>
- #endif
-
- #ifndef SOM_ODSemanticInterface_xh
- #include <SemtIntB.xh>
- #endif
-
- #ifndef SOM_ODWindowState_xh
- #include <WinStat.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- #ifndef _ISOSTR_
- #include <ISOStr.h>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- // -- Macintosh Includes --
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- // -- Textension Includes --
-
- #ifndef _Textension_
- #include "Textension.h"
- #endif
-
- #ifndef _TSMTextension_
- #include "TSMTextension.h"
- #endif
-
- #ifndef _QDTextRun_
- #include "QDTextRun.h"
- #endif
-
- #ifndef _TSMRun_
- #include "TSMRun.h"
- #endif
-
- #ifndef _RulerObject_
- #include "RulerObject.h"
- #endif
-
- #ifndef _RunObject_
- #include "RunObject.h"
- #endif
-
- #ifndef _LinkedFrames_
- #include "LinkedFrames.h"
- #endif
-
- #ifndef _AttrObject_
- #include "AttrObject.h"
- #endif
-
- #ifndef _FileStream_
- #include "FileStream.h"
- #endif
-
- // -- Script Runner --
-
- #ifndef SCRIPTRUNNERSUPPORT
- #include "ScriptRunnerSupport.h"
- #endif
-
- #ifndef SOM_PaletteExt_xh
- #include "PaletteExt.xh"
- #endif
-
-
- //------------------------------------------------------------------------------
- // Method: somInit
- // Origin: SOMObject
- //
- // Description: This is the SOM equivalent of a C++ class constructor.
- //
- // Warnings: You are not allowed to throw an exception from this method.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__somInit
- (
- SampleCode_TextEditor* somSelf
- )
- {
- SOMMethodDebug("TextEditor","somInit");
-
- // somInit and somUninit methods behave like C++ constructors in that the
- // inherited methods are called automatically. Thus, there is no need to
- // call the parent class' somInit or somUninit.
-
- // There is also no need to set instance variables to zero/NULL
- // since SOM guarantees that a newly constructed object is zeroed.
- }
-
- //------------------------------------------------------------------------------
- // Method: somUninit
- // Origin: SOMObject
- //
- // Description: This is the SOM equivalent of a C++ class destructor.
- //
- // Warnings: You are not allowed to throw an exception from this method.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__somUninit
- (
- SampleCode_TextEditor* somSelf
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","somUninit");
- }
-
- //------------------------------------------------------------------------------
- // Method: InitPart
- // Origin: ODPart
- //
- // Description: This method is called when a new instance of this part is being
- // created. The part should prepare itself to run.
- //
- // Warning: It is not appropriate to require user interaction while
- // stationery is being created. Do not present the user with error
- // dialogs or splash screens from this method.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__InitPart
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODStorageUnit* storageUnit,
- ODPart* partWrapper
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","InitPart");
-
- SOM_TRY
- // We must call the initialize method of our parent
- // class to allow OpenDoc to annotate our part's storageUnit, to
- // set our refcount, and to change our "initialized" flag to true.
- SampleCode_TextEditor_parent_ODPart_InitPart(somSelf, ev, storageUnit, partWrapper);
-
- // To allow editor swapping (translation) at runtime, OpenDoc requires
- // that we pass in a "reference" to ourselves when interacting with the
- // API (e.g. WindowState::RegisterWindow(), Dispatcher::RegisterIdle, etc).
- // The "partWrapper" passed to us here and in InitPartFromStorage is the
- // "reference" OpenDoc is asking us to use.
- _fSelf = partWrapper;
-
- // We are being created, either as part of generating stationery or
- // by some editor instantiating the part, so the destination storage
- // must be writeable.
- _fReadOnlyStorage = kODFalse;
-
- // Call the common initialization code to get set up.
- somSelf->Initialize(ev);
-
- // We need to set up a default preferred kind value for our
- // stationery file.
- _fPreferredKind = gGlobals->fNativeData;
-
- // Since we have just been created, our state/content info has
- // never been written out, so setting our "dirty" flag will
- // give us a chance to do that.
- somSelf->SetDirty(ev);
- SOM_CATCH_ALL
- // Clean up will occur in the destructor which will be called
- // shortly after we THROW the error.
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: InitPartFromStorage
- // Origin: ODPart
- //
- // Description: This method is called when a document/stationery is being opened
- // or when the part is internalized by its containing part. The
- // part should merely read in the saved state/content and
- // initialize itself. The part must not alter its storage unit;
- // otherwise, the "Save" menu item becomes enabled without the user
- // actually having made a change to the document.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__InitPartFromStorage
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODStorageUnit* storageUnit,
- ODPart* partWrapper
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","InitPartFromStorage");
-
- SOM_TRY
- // Call the initialize method of our parent PersistentObject class to generate
- // the appropriate time/date stamp on our part's storage unit.
- SampleCode_TextEditor_parent_ODPart_InitPartFromStorage(somSelf, ev, storageUnit, partWrapper);
-
- // To allow editor swapping (translation) at runtime, OpenDoc requires
- // that we pass in a reference to ourselves when interacting with the
- // API (e.g. WindowState::RegisterWindow(), Dispatcher::RegisterIdle, etc).
- // The "partWrapper" passed to us here and in InitPart is the
- // "reference" OpenDoc is asking us to use.
- _fSelf = partWrapper;
-
- // Are we being opened from a read-only draft? If so, we cannot
- // write anything back out to our storage unit.
- _fReadOnlyStorage = (ODGetDraft(ev, storageUnit)->
- GetPermissions(ev) < kODDPSharedWrite );
-
- // Call the common initialization code to get set up.
- somSelf->Initialize(ev);
-
- // Read in the state the part was in when it was last Externalized.
- // This allows the part to present the same environment the user
- // had the part set up in the last time it was displayed.
- somSelf->InternalizeStateInfo(ev, storageUnit);
-
- // The TextEditor needs to know about the "graphic" environment
- // before it internalizes the content. Internalization will occur
- // after the first facet is added.
-
- SOM_CATCH_ALL
- // Clean up will occur in the destructor which will be called
- // shortly after we THROW the error.
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: Initialize
- // Origin: TextEditor
- //
- // Description: This method is called during the initialization of the part. The
- // method is used to initialize all fields of the part and to
- // convert ISO types to tokens for faster comparisons throughout
- // the code.
- //
- // If an exception is thrown in this method, it will be propogated
- // back to OpenDoc which will call our ReleaseAll() method and the
- // class destructor. All memory allocated here will be cleaned up
- // in the ReleaseAll() method.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__Initialize
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","Initialize");
-
- SOM_TRY
- // Allow Textension to allocate its global structures and
- // analyze the runtime environment.
- somSelf->StartTextension(ev);
-
- // Grab a reference to the Session object. This is merely for
- // convenience.
- ODSession* session = ODGetSession(ev, somSelf);
-
- // Create a list to keep track of the facets we are being
- // displayed in. Also used for maintenance (i.e., Purging memory).
- _fDisplayFrames = new CList;
-
- if (gGlobalsUsageCount == 0)
- {
- gGlobals = new TextEditorGlobals;
-
- // We will be using the above foci (shared resources) in this
- // part. For convenience, we tokenize the values here and store
- // them for equivalence tests in the activation methods.
- gGlobals->fSelectionFocus = session->Tokenize(ev, kODSelectionFocus);
- gGlobals->fClipboardFocus = session->Tokenize(ev, kODClipboardFocus);
- gGlobals->fMenuFocus = session->Tokenize(ev, kODMenuFocus);
- gGlobals->fModalFocus = session->Tokenize(ev, kODModalFocus);
- gGlobals->fKeyFocus = session->Tokenize(ev, kODKeyFocus);
-
- // Tokenize our part's presentations and the view types we support.
- gGlobals->fMainPresentation = session->Tokenize(ev, kMainPresentation);
- gGlobals->fRulerPresentation = session->Tokenize(ev, kRulerPresentation);
-
- gGlobals->fFrameView = session->Tokenize(ev, kODViewAsFrame);
- gGlobals->fLargeIconView = session->Tokenize(ev, kODViewAsLargeIcon);
- gGlobals->fSmallIconView = session->Tokenize(ev, kODViewAsSmallIcon);
- gGlobals->fThumbnailView = session->Tokenize(ev, kODViewAsThumbnail);
-
- // This part supports PICT data files, so we must get the value types
- // OpenDoc associates with each. We do this through the Translation object.
- gGlobals->fScrapTextValue = session->GetTranslation(ev)->
- GetISOTypeFromPlatformType(ev, kTextDataKind, kODPlatformDataType);
- // Value type for TEXT scrap data
-
- gGlobals->fTextFileValue = session->GetTranslation(ev)->
- GetISOTypeFromPlatformType(ev, kTextFileKind, kODPlatformFileType);
- // Value type for TEXT files
-
- gGlobals->fStyledTextKind = session->GetTranslation(ev)->
- GetISOTypeFromPlatformType(ev, kSTXTDataKind, kODPlatformDataType);
- // Value type for 'stxt' styled text scrap data
-
- gGlobals->fAppleHFSFlavor = session->GetTranslation(ev)->
- GetISOTypeFromPlatformType(ev, 'hfs ', kODPlatformDataType);
- // Value type for file information
-
- // Tokenize the content value types that our part supports to
- // facilitate writing data in the preferred kind.
- gGlobals->fNativeData = session->Tokenize(ev, kTextEditorKind);
- gGlobals->fImportedData = session->Tokenize(ev, gGlobals->fScrapTextValue);
- gGlobals->fStyledTextData = session->Tokenize(ev, gGlobals->fStyledTextKind);
-
- _fPreferredKind = kODNullTypeToken;
-
- // We will also package the keyboard, menu, and selection focus
- // so that we can request the "set" at activation time.
- gGlobals->fUIFocusSet = session->GetArbitrator(ev)->CreateFocusSet(ev);
- gGlobals->fUIFocusSet->Add(ev, gGlobals->fMenuFocus);
- gGlobals->fUIFocusSet->Add(ev, gGlobals->fSelectionFocus);
- gGlobals->fUIFocusSet->Add(ev, gGlobals->fKeyFocus);
-
- // Determine what Script/Language the part is localized for.
- // This is important/necessary for creating OpenDoc's text objects.
- GetEditorScriptLanguage(ev, &gGlobals->fEditorsScript, &gGlobals->fEditorsLanguage);
-
- // Add the editor specific menu items.
- somSelf->LoadMenus(ev);
-
- // The first client of the global variables is running.
- gGlobalsUsageCount = 1;
- }
- else
- // If the globals have been initialized, we keep a "usage" count so that we
- // can null out the global variables when we are finished using them.
- {
- gGlobalsUsageCount++;
- }
-
- // Set default margins
- _fPageMargins.top = _fPageMargins.left = _fPageMargins.bottom
- = _fPageMargins.right = kDefaultDocMargin; /* pixels */
-
- // Set the document justification based on the script the part is
- // localized for.
- _fTextAlignment = (GetScriptVariable(gGlobals->fEditorsScript, smScriptJust) == 0)
- ? left : right;
-
- // ScriptRunner support.
- _fIsScriptRunnerOn = kODFalse;
- _fIsScriptRunnerHidden = kODFalse;
-
- // Printing.
- PrOpen();
- TRY
- if ( PrError() == noErr )
- {
- _fPrintRecord = ODNewHandle(sizeof(TPrint));
-
- PrintDefault((THPrint)_fPrintRecord);
-
- // Determine the current paper size and printable area
- // for the selected printer (or output device).
- _fPageSize = (**(THPrint)_fPrintRecord).prInfo.rPage;
- _fPaperSize = (**(THPrint)_fPrintRecord).rPaper;
- }
- else
- {
- // Set default page size to 8.5" x 11"
- // $$$$$ NOT LOCALIZABLE
- SetRect(&_fPageSize, 0, 0, 612, 792);
- SetRect(&_fPaperSize, 0, 0, 612, 792);
- }
- CATCH_ALL
- PrClose();
- RERAISE;
- ENDTRY
- PrClose();
- SOM_CATCH_ALL
- // Clean up will occur in the destructor which will be called
- // shortly after we THROW the error.
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: StartTextension
- // Origin: TextEditor
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__StartTextension
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","StartTextension");
-
- SOM_TRY
-
- OSErr error;
- ODUShort fontSize;
- Str255 fontName;
- ODSShort fontNum;
-
- // Start Textension. This gives it a chance to set up its global
- // tables and survey the machine. "True" tells Textension that we
- // will be installing AppleEvent handlers for TSM; it should not.
- error = CTSMTextension::TSMTextensionStart(kODTrue);
-
- // The "tsmAlreadyRegisteredErr" occurs if OpenDoc has created
- // a TSM document for this window already. We can ignore the error.
- if (error == tsmAlreadyRegisteredErr)
- error = noErr;
-
- THROW_IF_ERROR(error);
-
- CTSMRun* textRun = new CTSMRun;
- textRun->ITSMRun();
- CTextension::RegisterRun(textRun);
-
- CBasicRuler* ruler = new CBasicRuler;
- ruler->IBasicRuler();
- ruler->SetDefaults();
- CTextension::RegisterRuler(ruler);
-
- // set default font, font size
-
- {
- CUsingLibraryResources res;
- ReadPreferences(ev, ODGetSession(ev, somSelf), (StringPtr) &fontName,
- &fontSize);
- }
-
- GetFNum(fontName, &fontNum);
-
- CRunObject* defaultRun = CTextension::GetNewRunObject();
-
- defaultRun->SetAttributeValue(kFontAttr, &fontNum);
- defaultRun->SetAttributeValue(kFontSizeAttr, &fontSize);
-
- CTextension::SetDefaultRun(defaultRun);
-
- _fTextensionInitialized = kODFalse;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: InitializeTextension
- // Origin: TextEditor
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__InitializeTextension
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODBoolean isRoot
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","InitializeTextension");
-
- SOM_TRY
-
- TTextensionHandlers handlers;
- TSize approxDocSize;
-
- if ( isRoot )
- {
- // If we are the root part, then we need to present the user
- // with a "page/document".
-
- CPageFrames* pageFrames = new CPageFrames;
- pageFrames->IPageFrames();
-
- handlers.framesHandler = pageFrames;
-
- approxDocSize = kLargeSize; // Textension const
- }
- else
- {
- // If we are not the root part, then we need to support multiple
- // text blocks (unfortunately called "frames" by Textension) of
- // varying size and position.
-
- CVariableSizeFrames* varFrames = new CVariableSizeFrames;
- varFrames->IVariableSizeFrames();
-
- handlers.framesHandler = varFrames;
-
- approxDocSize = kMediumSize; // Textension const
- }
-
- _fTextension = new CTSMTextension;
- ((CTSMTextension*)_fTextension)->ITSMTextension(kODNULL, &handlers, approxDocSize);
-
- // Set up its autoscrolling (or lack thereof).
- _fTextension->SetAutoScroll(isRoot);
-
- _fTextensionInitialized = kODTrue;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: LoadMenus
- // Origin: TextEditor
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__LoadMenus
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","LoadMenus");
-
- SOM_TRY
-
- if (gGlobals->fMenuBar == kODNULL || !gGlobals->fMenuBar->IsValid(ev))
- {
- ODBoolean changingBaseMenuBar = (gGlobals->fMenuBar != kODNULL);
-
- if (changingBaseMenuBar)
- {
- // Remove our menus from the old menubar.
- for (ODSShort index = 0; index < kNumMenus; index++)
- {
- ODSShort id = kBaseMenuID + index;
- gGlobals->fMenuBar->RemoveMenu(ev, id);
- }
- }
-
- ODReleaseObject(ev, gGlobals->fMenuBar);
-
- // It is required that parts instantiate their menu bars from
- // the base OpenDoc menu bar. This maintains consistency in the
- // default menu items and their placement.
- // Since the object is a copy, we can add and subtract menus and
- // items without affecting other running parts.
- gGlobals->fMenuBar = ODGetSession(ev, somSelf)->GetWindowState(ev)->CopyBaseMenuBar(ev);
-
- // For convenience:
- ODMenuBar* mbar = gGlobals->fMenuBar;
-
- // Load and insert our resource-based menus.
-
- MenuHandle fontMenu;
- {
- CUsingLibraryResources res;
-
- fontMenu = GetMenu(kFontMenuID);
-
- // Remove all the old items before adding back on.
- while ( CountMItems(fontMenu) > 0 )
- {
- DeleteMenuItem(fontMenu, 1);
- }
-
- AppendResMenu(fontMenu, 'FONT');
-
- mbar->AddMenuLast(ev, kFontMenuID, fontMenu, _fSelf);
-
- // Add the other menus to the menubar.
- mbar->AddMenuLast(ev, kSizeMenuID, GetMenu(kSizeMenuID), _fSelf);
- mbar->AddMenuLast(ev, kStyleMenuID, GetMenu(kStyleMenuID), _fSelf);
- mbar->AddMenuLast(ev, kFormatMenuID, GetMenu(kFormatMenuID), _fSelf);
- mbar->AddMenuLast(ev, kToolsMenuID, GetMenu(kToolsMenuID), _fSelf);
- }
-
- // Issue command numbers for the items in the font menu.
- // The command number is equal to the FOND id + kBaseFontCmdID.
-
- for (int i=1; i <= CountMItems(fontMenu); i++)
- {
- Str255 fontName;
- ODSShort fontNum;
-
- GetMenuItemText(fontMenu, i, fontName);
- GetFNum(fontName, &fontNum);
-
- mbar->RegisterCommand(ev, kBaseFontCmdID + fontNum, kFontMenuID, i);
- }
-
- // Register command numbers for the Size, Style, and Format menus.
-
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 9, kSizeMenuID, 1);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 10, kSizeMenuID, 2);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 12, kSizeMenuID, 3);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 14, kSizeMenuID, 4);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 18, kSizeMenuID, 5);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 24, kSizeMenuID, 6);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 36, kSizeMenuID, 7);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 48, kSizeMenuID, 8);
- mbar->RegisterCommand(ev, kBaseFontSizeCmdID + 72, kSizeMenuID, 9);
- mbar->RegisterCommand(ev, kOtherFontSizeCmdID, kSizeMenuID,11);
-
- mbar->RegisterCommand(ev, kBaseFontStyleCmdID + normal, kStyleMenuID, 1);
- mbar->RegisterCommand(ev, kBaseFontStyleCmdID + bold, kStyleMenuID, 3);
- mbar->RegisterCommand(ev, kBaseFontStyleCmdID + italic, kStyleMenuID, 4);
- mbar->RegisterCommand(ev, kBaseFontStyleCmdID + underline, kStyleMenuID, 5);
- mbar->RegisterCommand(ev, kBaseFontStyleCmdID + outline, kStyleMenuID, 6);
-
- mbar->RegisterCommand(ev, kShowHideRulerCmdID, kFormatMenuID, 1);
- mbar->RegisterCommand(ev, kSettingsCmdID, kFormatMenuID, 3);
-
- mbar->RegisterCommand(ev, kToggleScriptRunnerCmdID, kToolsMenuID, 1);
-
- // If we're a read-only part, disable the font menus.
- mbar->EnableCommand(ev, mbar->GetCommand(ev, kFontMenuID, 0), !_fReadOnlyStorage);
- mbar->EnableCommand(ev, mbar->GetCommand(ev, kSizeMenuID, 0), !_fReadOnlyStorage);
- mbar->EnableCommand(ev, mbar->GetCommand(ev, kStyleMenuID, 0), !_fReadOnlyStorage);
- mbar->EnableCommand(ev, mbar->GetCommand(ev, kFormatMenuID, 0), !_fReadOnlyStorage);
- mbar->EnableCommand(ev, mbar->GetCommand(ev, kToolsMenuID, 0), !_fReadOnlyStorage);
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: Release
- // Origin: ODRefCntObj
- //
- // Description: This method is called each time an object releases a reference
- // to the part. If the refcount falls to 0, the part should
- // release the fSelf part reference.
- //
- // Warning: If the part releases any other object when the refcount falls to
- // zero, it will need to override the Acquire method so
- // that the object can be referenced again if the parts refcount
- // should be incremented before it is deleted.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorStorage
-
- SOM_Scope void
- SOMLINK TextEditor__Release
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","Release");
-
- SOM_TRY
-
- SampleCode_TextEditor_parent_ODPart_Release(somSelf, ev);
-
- if (somSelf->GetRefCount(ev) == 0)
- ODGetDraft(ev, somSelf)->ReleasePart(ev, _fSelf);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: ReleaseAll
- // Origin: ODPart
- //
- // Description: This method is called just prior to the part being deleted by
- // the Draft. The part must release all references to all
- // refcounted objects it has stored internally; not doing so, will
- // cause an "invalid ref count" exception/error at some later time.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorStorage
-
- SOM_Scope void
- SOMLINK TextEditor__ReleaseAll
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","ReleaseAll");
-
- SOM_TRY
-
- // Release the reference counted objects we are holding onto.
-
- // Release our extensions. Note: we can't use ODReleaseObject or
- // ODFinalReleaseObject here because they null out the field before the
- // call to Release, but we need the field intact when ReleaseExtension is
- // called back upon the last reference being released.
- ODRelease(ev, _fSemanticIntf);
- ODRelease(ev, _fSettingsExt);
- ODRelease(ev, _fTextTransferExt);
-
- // Release this instance's Textension interface.
- if ( _fTextension )
- _fTextension->Free();
-
- // Release the ScriptRunner extension, if we have acquired it.
- ODReleaseObject(ev, _fScriptPaletteExt);
-
- // If the last part instance using the globals is released,
- // we need to NULL out the globals.
-
- if ( --gGlobalsUsageCount == 0 )
- {
- // On the other hand, the FocusSet is not shared and deleting it
- // is the right thing to do.
- ODDeleteObject(gGlobals->fUIFocusSet);
-
- // We can't shutdown Textension until all instances of the
- // Text Editor are finished using it.
- CTSMTextension::TSMTextensionTerminate();
-
- ODDisposePtr(gGlobals->fScrapTextValue);
- ODDisposePtr(gGlobals->fTextFileValue);
- ODDisposePtr(gGlobals->fAppleHFSFlavor);
-
- // We need to remove our menus from the menubar and dispose of
- // them to free up their memory.
- {
- CUsingLibraryResources res;
- for (ODSShort index = 0; index < kNumMenus; index++)
- {
- ODSShort id = kBaseMenuID + index;
-
- MenuHandle menu = gGlobals->fMenuBar->GetMenu(ev, id);
- gGlobals->fMenuBar->RemoveMenu(ev, id);
- if (menu)
- ReleaseResource((Handle) menu);
- }
- }
-
- ODReleaseObject(ev, gGlobals->fMenuBar);
-
- // Dispose the thumbnail picture.
- if ( gGlobals->fThumbnail != kODNULL )
- ReleaseResource(gGlobals->fThumbnail);
-
- // Delete the globals struct.
- ODDeleteObject(gGlobals);
- }
-
- // Remove and release our display frames.
- if ( _fDisplayFrames )
- {
- CListIterator fiter(_fDisplayFrames);
- for ( CFrameProxy* proxy = (CFrameProxy*) fiter.First();
- fiter.IsNotComplete(); proxy = (CFrameProxy*) fiter.Next() )
- {
- // Delete the proxy object and its contents. The frame's
- // refcount will be decremented in the proxy destructor.
- fiter.RemoveCurrent();
- delete proxy;
- }
-
- // Delete the display frame collection.
- ODDeleteObject(_fDisplayFrames);
- }
-
- SampleCode_TextEditor_parent_ODPart_ReleaseAll(somSelf, ev);
-
- SOM_CATCH_ALL
- // If something goes wrong while we are cleaning up, we must
- // let the Draft now because there may be some refcounted objects
- // which did not get released. Not to mention, possible memory
- // leaks.
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: Purge
- // Origin: ODObject
- //
- // Description: This method is called when the OpenDoc requires more memory for
- // allocating objects and just before a part is deleted. The part
- // should free up as much memory as it can.
- //
- // The part determines which views are being "used" in its display
- // frames. The resources for the unused view types are then purged.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorStorage
-
- SOM_Scope ODSize
- SOMLINK TextEditor__Purge
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODSize size
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","Purge");
-
- ODSize bytesFreed = 0;
-
- SOM_TRY
-
- ODBoolean usingThumbnail = kODFalse;
-
- // Iterate over the frames we are displayed through and determine which
- // view types are currently in use.
-
- if ( _fDisplayFrames != kODNULL )
- {
- CListIterator fiter(_fDisplayFrames);
- for ( CFrameProxy* proxy = (CFrameProxy*) fiter.First();
- fiter.IsNotComplete(); proxy = (CFrameProxy*) fiter.Next() )
- {
- // If the display frame is real (has been "connected" or was "added")
- // get its view type; otherwise, ignore it.
- if ( proxy->FrameIsLoaded(ev) )
- {
- ODTypeToken frameView = proxy->GetFrame(ev)->GetViewType(ev);
-
- if ( frameView == gGlobals->fThumbnailView )
- usingThumbnail = kODTrue;
-
- // Release the frame reference, but don't get rid of the
- // proxy object because we're not done with the frame. If
- // all parts release their references the frame will be
- // purged from memory.
- proxy->Purge(ev);
- }
- }
- }
-
- // Based on the usage of the supported view types, free up as much
- // memory as possible.
-
- if ( !usingThumbnail && gGlobals->fThumbnail != kODNULL )
- {
- bytesFreed += ODGetHandleSize(gGlobals->fThumbnail);
- ReleaseResource(gGlobals->fThumbnail);
- gGlobals->fThumbnail = kODNULL;
- }
-
- bytesFreed += SampleCode_TextEditor_parent_ODPart_Purge(somSelf, ev, size);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return bytesFreed;
- }
-
- //------------------------------------------------------------------------------
- // Method: HandleChange
- // Origin: TextEditor
- //
- // Description: This method is called by the part when the content or state of
- // the part has been modified by the user and the "Save" menu item
- // should be enabled.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorStorage
-
- SOM_Scope void
- SOMLINK TextEditor__HandleChange
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","HandleChange");
-
- SOM_TRY
-
- // Tell ourselves that we're now dirty.
- somSelf->SetDirty(ev);
-
- ODUpdateID updateID = ODGetSession(ev, somSelf)->UniqueUpdateID(ev);
-
- // Iterate over our display frames.
- CListIterator fiter(_fDisplayFrames);
- for ( CFrameProxy* proxy = (CFrameProxy*) fiter.First();
- fiter.IsNotComplete(); proxy = (CFrameProxy*) fiter.Next() )
- {
- // If the display frame is real (has been "connected" or was "added")
- // call ContentUpdated for the frame; otherwise, ignore it.
- if ( proxy->FrameIsLoaded(ev) )
- {
- ODFrame* frame = proxy->GetFrame(ev);
-
- // Call ContentUpdated to tell any potential container
- // that we have made a change to our content in case we
- // are in a link.
- frame->ContentUpdated(ev, updateID);
- }
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: HasExtension
- // Origin: ODObject
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope ODBoolean
- SOMLINK TextEditor__HasExtension
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODType extensionName
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","HasExtension");
-
- ODBoolean result = kODFalse;
-
- TRY
-
- if ( ODISOStrEqual(extensionName, kODExtSemanticInterface)
- || (ODISOStrEqual(extensionName, kODSettingsExtension) && !_fReadOnlyStorage)
- || ODISOStrEqual(extensionName, kDataTransferExtension)
- || ODISOStrEqual(extensionName, kTextTransferExtension) )
- {
- return kODTrue;
- }
- else
- {
- result = SampleCode_TextEditor_parent_ODPart_HasExtension(somSelf, ev, extensionName);
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // Method: AcquireExtension
- // Origin: ODObject
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope ODExtension*
- SOMLINK TextEditor__AcquireExtension
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODType extensionName
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","AcquireExtension");
-
- ODExtension* result = kODNULL;
-
- SOM_TRY
-
- if ( ODISOStrEqual(extensionName, kODExtSemanticInterface) )
- {
- if ( !_fSemanticIntf )
- {
- ODSession* session = ODGetSession(ev, somSelf);
-
- _fSemanticIntf = new TextEditorSemIntf;
- THROW_IF_NULL(_fSemanticIntf); // SOM constructors don’t throw.
- _fSemanticIntf->InitSemanticInterface(ev, _fSelf, session);
- _fSemanticIntf->RegisterTextension(ev, _fTextension);
- }
-
- ODAcquireObject(ev, _fSemanticIntf);
- result = _fSemanticIntf;
- }
- else if ( ODISOStrEqual(extensionName, kODSettingsExtension) )
- {
- if ( !_fSettingsExt )
- {
- _fSettingsExt = new TextEditorSettingsExt;
- THROW_IF_NULL(_fSettingsExt);
- _fSettingsExt->InitSettingsExtension(ev, _fSelf);
- }
-
- ODAcquireObject(ev, _fSettingsExt);
- result = _fSettingsExt;
- }
- else if ( ODISOStrEqual(extensionName, kDataTransferExtension)
- || ODISOStrEqual(extensionName, kTextTransferExtension) )
- {
- if ( !_fTextTransferExt )
- {
- _fTextTransferExt = new TextEditorTransferExt;
- THROW_IF_NULL(_fTextTransferExt);
- _fTextTransferExt->InitTextEditorTransferExt(ev, _fSelf);
- }
-
- ODAcquireObject(ev, _fTextTransferExt);
- result = _fTextTransferExt;
- }
- else
- {
- result = SampleCode_TextEditor_parent_ODPart_AcquireExtension(somSelf, ev, extensionName);
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return result;
- }
-
-
- //------------------------------------------------------------------------------
- // Method: ReleaseExtension
- // Origin: ODObject
- //
- // Description: This method is called during the initialization of the part.
- //------------------------------------------------------------------------------
- #pragma segment TextEditorInitialization
-
- SOM_Scope void
- SOMLINK TextEditor__ReleaseExtension
- (
- SampleCode_TextEditor* somSelf,
- Environment* ev,
- ODExtension* extension
- )
- {
- SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
- SOMMethodDebug("TextEditor","ReleaseExtension");
-
- SOM_TRY
-
- if ( ODObjectsAreEqual(ev, extension, _fSemanticIntf) )
- {
- ODDeleteObject(_fSemanticIntf);
- }
- else if ( ODObjectsAreEqual(ev, extension, _fSettingsExt) )
- {
- ODDeleteObject(_fSettingsExt);
- }
- else if ( ODObjectsAreEqual(ev, extension, _fTextTransferExt) )
- {
- ODDeleteObject(_fTextTransferExt);
- }
- else
- {
- SampleCode_TextEditor_parent_ODPart_ReleaseExtension(somSelf, ev, extension);
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
-
-